<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/11/22 0022
 * Time: 10:15
 */

namespace app\admin\controller;
use think\Session;
use think\Controller;
use think\Db;
use think\Model;



class Consult  extends Controller
{
    public function _initialize(){
        if(!session('id')){
            $this->redirect('/admin/index/index');
        }
    }
    //咨询列表
    public function index(){
        $user =  Model('consult');
        $data = $user->getlist();
        $this->assign('data',$data);
        return $this->fetch();
    }
    //处理
    public function consultdeal(){
         $id = intval(input('post.id'));
        if($id){
            $data['status'] =1;
            $result = Db::name('consult')->where('id',$id)->update($data);
        }else{
            return json(['code'=>0,'msg'=>'参数错误']);
        }
        if($result !==false){
            return json(['code'=>1,'msg'=>'操作成功']);
        }else{
            return json(['code'=>0,'msg'=>'操作失败']);
        }


    }
    //删除
    public function consultdel(){
        $id = intval(input('post.id'));
        if($id){
            $res = Db::name('consult')->where('id',$id)->update(['is_del'=>1]);
        }
        if($res){
            return json(['code'=>1,'msg'=>'操作成功']);
        }else{
            return json(['code'=>0,'msg'=>'操作失败']);
        }
    }

    //咨询配置
    public function config(){
        $where['is_show'] = 1;
        $where['sign'] = "pc";
        $data = Db::name('consult_config')->where($where)->find();
        if($data['ps_style']){
            $data['ps_style'] = explode(',',$data['ps_style']);
        }
        if($data['clothes_num']){
            $data['clothes_num'] = explode(',',$data['clothes_num']);
        }
        if($data['xg_num']){
            $data['xg_num'] = explode(',',$data['xg_num']);
        }
        if($data['xc_num']){
            $data['xc_num'] = explode(',',$data['xc_num']);
        }



        $this->assign('data',$data);
        return $this->fetch();
    }

    //添加
    public function add_config(){
        $id = intval(input('param.id'));
        $data =[
            'title'=>input('post.title'),
            'is_show'=>input('post.is_show'),
            'sign'=>"pc"
        ];
        if(isset($_POST['ps_style'] ) && $_POST['ps_style'] !==''){
            $data['ps_style'] = implode(',',$_POST['ps_style']);
        }
        if(isset($_POST['clothes_num'] ) && $_POST['clothes_num'] !==''){
            $data['clothes_num'] = implode(',',$_POST['clothes_num']);
        }
        if(isset($_POST['xg_num'] ) && $_POST['xg_num'] !==''){
            $data['xg_num'] = implode(',',$_POST['xg_num']);
        }
        if(isset($_POST['xc_num'] ) && $_POST['xc_num'] !==''){
            $data['xc_num'] = implode(',',$_POST['xc_num']);
        }

        $file = request()->file('consult_banner');
        if($file){
            $file_info =$file->move('./uploads/config/');
            $data['consult_banner'] ='/uploads/config/'.$file_info->getSaveName();
        }
        if($id){
            $result = Db::name('consult_config')->where('id',$id)->update($data);

        }else{
            $data['create_time'] =time();
            $result = Db::name('consult_config')->insert($data);

        }
        if($result !==false){
            return json(['code'=>1,'msg'=>'操作成功']);
        }else{
            return json(['code'=>0,'msg'=>'操作失败']);

        }
    }




}